package com.jinlejia.erp.ui.new_house.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.databinding.ItemHouseIntroduceBinding;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/**
 * @author SongJiaTai
 * @description:楼盘介绍Adapter
 * @date :2020/8/14 18:37
 */
public class HouseIntroduceAdapter extends YfListAdapter<Map<String,Object>> {
    private Context mContext;

    public HouseIntroduceAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_house_introduce,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemHouseIntroduceBinding binding = DataBindingUtil.getBinding(holder.itemView);
        Map<String, Object> map = mData.get(position);

        Iterator iter1 = map.entrySet().iterator();
        String houseImge = "";
        String houseInfoText = "";
        while(iter1.hasNext()){
            Map.Entry<String,String> entry=(Map.Entry<String,String>)iter1.next();
            houseImge = String.valueOf(entry.getKey());
            houseInfoText = String.valueOf(entry.getValue());
        }
        Glide.with(mContext).load(ApiConfig.MAGEHEADER_URL().concat(houseImge))
                .placeholder(R.mipmap.ic_no_img)
                .error(R.mipmap.ic_no_img)
                .into(binding.imgHouseInfoMessage);

        binding.tvHouseInfoMessage.setText(houseInfoText);
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    private static final class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}
